// pages/emgResponse/editSummary/editSummary.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      //当前应急响应ID
      emergencyId:'',
      //起始应急ID
      startId:'',
      //当前应急名称
      name:'',
      // 是否已有数据
      hadFlag:false,
      //应急总结数据
      emerSummary:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
          emergencyId:options.emergencyId,
          startId:options.startId,
          name:options.name,
          'emerSummary.responseLevel':options.typeLevel
      });
      this.searchSummary();
  },

 //查找原有的工作总结
    searchSummary:function () {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={companyId:app.globalData.user.companyId,planPublicId:this.data.emergencyId};
        that.setData({
            hadFlag:false
        });

        wx.showLoading()
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgSummary/find',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                if(res.data.data.list.length){
                    that.setData({
                        hadFlag:true,
                        emerSummary:res.data.data.list[0]
                    })
                    //关闭加载
                    wx.hideLoading()
                }else{
                    that.getTimeRange();
                    //关闭加载
                    wx.hideLoading()
                }
            },
            fail(){
                //关闭加载
                wx.hideLoading()
            }
        })
    },
    //获取时间范围
    getTimeRange:function () {
        let that = this
        let header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={startPlanPublicId:this.data.startId,planPublicId:this.data.emergencyId};
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgSummary/getSummaryInitInfo',
            header: header,
            method: "POST",
            data: params,
            success(res) {
                that.setData({
                    'emerSummary.timeRange':res.data.data.timeRange
                })
            },
            fail(){
                wx.showToast({
                    title: '获取起止时间失败',
                    icon: 'none'
                })
            }
        })

    },
    //保存总结
    saveSummary:function () {
        let that = this
        let header;
        let urlStr;
        header = {
            'Cookie': wx.getStorageSync("sessionid") ,//读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };
        let params={
            deptId:app.globalData.user.deptId,
            companyId:app.globalData.user.companyId,
            timeRange:this.data.emerSummary.timeRange,
            responseLevel:this.data.emerSummary.responseLevel,
            responseSituation:this.data.emerSummary.responseSituation,
            effectEvaluation:this.data.emerSummary.effectEvaluation,
            damagedUnit:this.data.emerSummary.damagedUnit,
            deathSituation:this.data.emerSummary.deathSituation,
            econLossSituation:this.data.emerSummary.econLossSituation,
            econLossEstimate:this.data.emerSummary.econLossEstimate,
            planPublicId:this.data.emergencyId,
        };

        if(this.data.hadFlag){
            urlStr='emgSummary/update';
            params.id=this.data.emerSummary.id;
        }else{
            urlStr='emgSummary/add';
        }

        wx.showLoading()
        wx.request({
            url: app.globalData.baseUrl + '/safe/' + urlStr,
            header: header,
            method: "POST",
            data: params,
            success(res) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success'
                })
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1
                    })},1000);
                //关闭加载
                wx.hideLoading()
            },
            fail(){
                //关闭加载
                wx.hideLoading()
            }
        })
    },

    //输入框数据同步
    timeRangeBlur:function (e) {
        this.setData({
            'emerSummary.timeRange':e.detail.value
        })
    },
    responseLevelBlur:function (e) {
        this.setData({
            'emerSummary.responseLevel':e.detail.value
        })
    },
    responseSituationBlur:function (e) {
        this.setData({
            'emerSummary.responseSituation':e.detail.value
        })
    },
    effectEvaluationBlur:function (e) {
        this.setData({
            'emerSummary.effectEvaluation':e.detail.value
        })
    },
    damagedUnitBlur:function (e) {
        this.setData({
            'emerSummary.damagedUnit':e.detail.value
        })
    },
    deathSituationBlur:function (e) {
        this.setData({
            'emerSummary.deathSituation':e.detail.value
        })
    },
    econLossSituationBlur:function (e) {
        this.setData({
            'emerSummary.econLossSituation':e.detail.value
        })
    },
    econLossEstimateBlur:function (e) {
        this.setData({
            'emerSummary.econLossEstimate':e.detail.value
        })
    }

})